function [ distance ] = Mutual_Informaion( image1,image2 )
%MUTUAL_INFORMAION Summary of this function goes here
%   Detailed explanation goes here
i1=image1;i2=image2;
image1=i1;
image2=i2;
image1 = load_nifti(image1);
image2 = load_nifti(image2);

im1 = image1(image1>=1 & image1 <=1000);
im1 = im1(:)./10;
hist1 = hist(im1,100);
p1 = hist1 ./ sum(hist1);


im2 = image2(image2>=1 & image2 <=1000);
im2 = im2(:)./10;
hist2 = hist(im2,100);
p2 = hist2 ./ sum(hist2);

hist2d = zeros(100,100);

for i=1:size(image1,1)
    for j=1:size(image1,2)
        for k=1:size(image1,3)
            im1val = image1(i,j,k);
            im2val = image2(i,j,k);
            if (im1val<1 || im1val>1000 || im2val<1 || im2val>1000)
                continue;
            end
            hist2d(ceil(im1val/10),ceil(im2val/10)) = hist2d(ceil(im1val/10),ceil(im2val/10)) + 1;
        end
    end
end

p = hist2d./sum(hist2d(:));

distance = 0;

for j=1:size(p2,2)
    for i=1:size(p1,2)
        if (p(i,j)==0)
            continue;
        end
        distance = distance + p(i,j)*log(p(i,j)/(p1(i)*p2(j)));
    end
 end

end

